package com.sdlh.demo.springboot.common.mqtt;

import com.sdlh.demo.springboot.common.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/mqtt")
public class MqttTestController {

    @Autowired
    private MQTTConnect mqttConnect;

    @ResponseBody
    @PostMapping("/mqttTest01")
    public ResponseResult<Object> test(@RequestParam("msg") String msg,
                                       @RequestParam("topic") String topic,
                                       @RequestParam("qos") int qos) throws Exception {
        if (StringUtils.isBlank(topic)){
            topic =MQTTConnect.TOPIC;
        }
        mqttConnect.pub(topic, msg,qos);
        return ResponseResult.success("ok");
    }
}
